package com.zenithmind.library.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zenithmind.library.pojo.domain.BookStatistics;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;

/**
 * 图书统计Mapper接口
 * 遵循接口隔离原则：只包含统计相关的数据访问方法
 * 
 * @author ZenithMind
 * @since 2024-06-14
 */
@Mapper
public interface BookStatisticsMapper extends BaseMapper<BookStatistics> {

    /**
     * 根据图书ID查询统计信息
     * 
     * @param bookId 图书ID
     * @return 统计信息
     */
    @Select("SELECT * FROM lib_book_statistics WHERE book_id = #{bookId} AND deleted = 0")
    BookStatistics selectByBookId(@Param("bookId") String bookId);

    /**
     * 增加借阅次数
     * 
     * @param bookId 图书ID
     * @return 更新行数
     */
    @Update("UPDATE lib_book_statistics SET " +
            "total_borrow_count = total_borrow_count + 1, " +
            "current_borrow_count = current_borrow_count + 1, " +
            "last_borrow_time = NOW(), " +
            "update_time = NOW() " +
            "WHERE book_id = #{bookId} AND deleted = 0")
    int incrementBorrowCount(@Param("bookId") String bookId);

    /**
     * 减少当前借阅次数（归还时调用）
     * 
     * @param bookId 图书ID
     * @return 更新行数
     */
    @Update("UPDATE lib_book_statistics SET " +
            "current_borrow_count = current_borrow_count - 1, " +
            "update_time = NOW() " +
            "WHERE book_id = #{bookId} AND current_borrow_count > 0 AND deleted = 0")
    int decrementCurrentBorrowCount(@Param("bookId") String bookId);

    /**
     * 增加预约次数
     * 
     * @param bookId 图书ID
     * @return 更新行数
     */
    @Update("UPDATE lib_book_statistics SET " +
            "total_reservation_count = total_reservation_count + 1, " +
            "current_reservation_count = current_reservation_count + 1, " +
            "last_reservation_time = NOW(), " +
            "update_time = NOW() " +
            "WHERE book_id = #{bookId} AND deleted = 0")
    int incrementReservationCount(@Param("bookId") String bookId);

    /**
     * 减少当前预约次数
     * 
     * @param bookId 图书ID
     * @return 更新行数
     */
    @Update("UPDATE lib_book_statistics SET " +
            "current_reservation_count = current_reservation_count - 1, " +
            "update_time = NOW() " +
            "WHERE book_id = #{bookId} AND current_reservation_count > 0 AND deleted = 0")
    int decrementCurrentReservationCount(@Param("bookId") String bookId);

    /**
     * 增加评价
     * 
     * @param bookId 图书ID
     * @param isPositive 是否好评
     * @return 更新行数
     */
    @Update("UPDATE lib_book_statistics SET " +
            "review_count = review_count + 1, " +
            "positive_review_count = positive_review_count + #{isPositive}, " +
            "negative_review_count = negative_review_count + #{isNegative}, " +
            "last_review_time = NOW(), " +
            "update_time = NOW() " +
            "WHERE book_id = #{bookId} AND deleted = 0")
    int addReview(@Param("bookId") String bookId, 
                  @Param("isPositive") Integer isPositive, 
                  @Param("isNegative") Integer isNegative);

    /**
     * 移除评价
     * 
     * @param bookId 图书ID
     * @param wasPositive 是否为好评
     * @return 更新行数
     */
    @Update("UPDATE lib_book_statistics SET " +
            "review_count = review_count - 1, " +
            "positive_review_count = positive_review_count - #{wasPositive}, " +
            "negative_review_count = negative_review_count - #{wasNegative}, " +
            "update_time = NOW() " +
            "WHERE book_id = #{bookId} AND review_count > 0 AND deleted = 0")
    int removeReview(@Param("bookId") String bookId, 
                     @Param("wasPositive") Integer wasPositive, 
                     @Param("wasNegative") Integer wasNegative);

    /**
     * 更新评分
     * 
     * @param bookId 图书ID
     * @param rating 评分
     * @return 更新行数
     */
    @Update("UPDATE lib_book_statistics SET " +
            "rating = #{rating}, " +
            "update_time = NOW() " +
            "WHERE book_id = #{bookId} AND deleted = 0")
    int updateRating(@Param("bookId") String bookId, @Param("rating") BigDecimal rating);

    /**
     * 增加浏览次数
     * 
     * @param bookId 图书ID
     * @return 更新行数
     */
    @Update("UPDATE lib_book_statistics SET " +
            "view_count = view_count + 1, " +
            "update_time = NOW() " +
            "WHERE book_id = #{bookId} AND deleted = 0")
    int incrementViewCount(@Param("bookId") String bookId);

    /**
     * 增加收藏次数
     * 
     * @param bookId 图书ID
     * @return 更新行数
     */
    @Update("UPDATE lib_book_statistics SET " +
            "favorite_count = favorite_count + 1, " +
            "update_time = NOW() " +
            "WHERE book_id = #{bookId} AND deleted = 0")
    int incrementFavoriteCount(@Param("bookId") String bookId);

    /**
     * 减少收藏次数
     * 
     * @param bookId 图书ID
     * @return 更新行数
     */
    @Update("UPDATE lib_book_statistics SET " +
            "favorite_count = favorite_count - 1, " +
            "update_time = NOW() " +
            "WHERE book_id = #{bookId} AND favorite_count > 0 AND deleted = 0")
    int decrementFavoriteCount(@Param("bookId") String bookId);

    /**
     * 增加分享次数
     * 
     * @param bookId 图书ID
     * @return 更新行数
     */
    @Update("UPDATE lib_book_statistics SET " +
            "share_count = share_count + 1, " +
            "update_time = NOW() " +
            "WHERE book_id = #{bookId} AND deleted = 0")
    int incrementShareCount(@Param("bookId") String bookId);
}
